package com.zhangxiaofanfan.hotcode.second;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode088 {
    public static void main(String[] args) {
        HotCode088 hotCode = new HotCode088();
        int[] nums = {2,-5,-2,-4,3};
        System.out.println(hotCode.maxProduct(nums));
    }

    public int maxProduct(int[] nums) {
        if (nums.length == 0) {
            throw new RuntimeException("入参数组大小为0, 不符合要求");
        }
        int max = nums[0];
        Integer[] dp = new Integer[2];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                dp[0] = null;
                dp[1] = null;
                max = Math.max(max, nums[i]);
            } else if (nums[i] > 0) {
                dp[0] = dp[0] == null ? nums[i] : nums[i] * dp[0];
                dp[1] = dp[1] == null ? null : nums[i] * dp[1];
                max = Math.max(max, dp[0]);
            } else {
                if (dp[0] == null && dp[1] == null) {
                    dp[1] = nums[i];
                } else if (dp[0] == null) {
                    dp[0] = dp[1] * nums[i];
                    dp[1] = nums[i];
                } else if (dp[1] == null){
                    dp[1] = dp[0] * nums[i];
                    dp[0] = null;
                } else {
                    int temp = dp[1];
                    dp[1] = dp[0] * nums[i];
                    dp[0] = temp * nums[i];
                }
                max = dp[0] == null ? max : Math.max(max, dp[0]);
            }
        }
        return max;
    }
}